package com.android.shuttle.chart.xlchart;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.shuttle.chart.R;

import java.util.ArrayList;
import java.util.Random;

public class GridLineActivity extends AppCompatActivity {
    private AxisMode currentAxisMode = AxisMode.XY;
    private GridLineView traceView;
    private Random random = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xlchart);
        // traceview
        traceView = (GridLineView) findViewById(R.id.traceView);
        // axis - changemode
        TextView tvAxis = (TextView) findViewById(R.id.tv_axis);
        tvAxis.setTextColor(Color.GREEN);
        tvAxis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currentAxisMode = AxisMode.getAxisMode(currentAxisMode);
                TextView tvAxis = (TextView) view;
                Log.e("AxisMode", currentAxisMode.getMode() + "");
                switch (currentAxisMode.getMode() % 3) {
                    case 0:
                        tvAxis.setText("X-Y");
                        tvAxis.setTextColor(Color.GREEN);
                        break;
                    case 1:
                        tvAxis.setText("X-Z");
                        tvAxis.setTextColor(Color.BLACK);
                        break;
                    case 2:
                        tvAxis.setText("Y-Z");
                        tvAxis.setTextColor(Color.RED);
                        break;
                    default:
                        tvAxis.setText("X-Y");
                        tvAxis.setTextColor(Color.GREEN);
                        break;
                }
                traceView.changeAxisMode(currentAxisMode);
            }
        });
        // add data 随意添加一个在区域内的点
        findViewById(R.id.btn_add_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addData();
            }
        });
        // initData
        initData();
    }

    private void addData() {
        double x = 0, y = 0, z = 0;
        x = getRandom();
        y = getRandom();
        z = getRandom();
        LocateD newPos = new LocateD(x, y, z);
        traceView.addSingleData(newPos);
    }

    /**
     * 随机生成10个点
     */
    private void initData() {
        ArrayList<LocateD> locateDList = new ArrayList<>();
        double x = 0, y = 0, z = 0;
        for (int i = 0; i < 10; i++) {
            x = getRandom();
            y = getRandom();
            z = getRandom();
            LocateD locateD = new LocateD(x, y, z);
            locateDList.add(locateD);
        }
        traceView.addDataSet(locateDList);
    }

    /**
     * 生成 (-900,900) 区域内的随机int值
     *
     * @return
     */
    private int getRandom() {
        return random.nextInt(900 - (-900) + 1) + (-900);
    }
}
